package com.woniu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.pojo.Book;
import com.woniu.pojo.BookType;
import com.woniu.service.BookService;
import com.woniu.service.BookTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping(value = "/book")
@RestController
@Api(tags = "书籍")
@Slf4j
public class BookController {
    @Autowired
    private BookTypeService bookTypeService;
    @Autowired
    private BookService bookService;


    /**
     *  查询所有书籍,分页查询,按照销量降序
     *
     */

    @GetMapping
    @ApiOperation(value = "查询所有书籍")
    public ResponseEntity<List<Book>> pageBook(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "5") Integer size){
        List<Book> types = bookService.pageBook(page,size);
        return ResponseEntity.ok(types);
    }

    /**
     * 查询热销榜数据，分页显示
     *
     * */
    @PostMapping("/top")
    @ApiOperation(value = "查询热销榜数据")
    public ResponseEntity<Page<Book>> findTopSales(@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "5") Integer size){
        Page<Book> topSales = bookService.findTopSales(page, size);
        return ResponseEntity.ok(topSales);
    }

    /**
     * 按照书籍类型 显示，分页
     * */
    @PostMapping("/findByType")
    @ApiOperation(value = "按照书籍类型显示")
    public ResponseEntity<Page<Book>> findByType(@RequestBody Map<String, Integer> pageParams) {
        System.out.println(pageParams);
        log.info("按照书籍类型显示");
        Page<Book> page = bookTypeService.findBytype(pageParams.get("pageNum"), pageParams.get("pageSize"),pageParams.get("typeId"));
        return ResponseEntity.ok(page);
    }

    /**
     * 通过ID查询书籍详情，回显信息
     */
    @GetMapping("/findById/{id}")
    @ApiOperation(value = "通过ID查询书籍详情")
    public ResponseEntity<Book> findById(@PathVariable("id") Long id) {
        Book book = bookService.findById(id);
        return ResponseEntity.ok(book);
    }

}
